import React, { Component } from 'react'

import moment from 'moment'
export default class Clock extends Component {

  state={
    now:0
  }
  timer=null

  runClock=()=>{
    this.timer=setInterval(() => {
      this.setState({now:Date.now()})
    }, 1000);
  }

  componentDidMount(){
    if(this.timer){
      clearInterval(this.timer)
      this.timer=null
    }
    this.setState({now:Date.now()})
    this.runClock()
  }

  componentWillUnmount(){
    clearInterval(this.timer)
    this.timer=null
  }

  render() {
    const {now} =this.state
    return (
      <span>
        {moment(now).format("YYYY-MM-DD hh:mm:ss")}
      </span>
    )
  }
}
